package com.ht.controller;

import java.util.Arrays;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.entity.ColumnsEntity;
import com.ht.service.IColumnsService;
import com.ht.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ht
 * @email ht035200@163.com
 * @date 2021-09-24 16:30:53
 */
@RestController
@RequestMapping("/generator/columns")
public class ColumnsController {
    @Autowired
    private IColumnsService columnsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(name = "page", defaultValue = "1") Integer pageNo, @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "tableName", required = false) String tableName) {
        return R.ok().put("page", columnsService.queryPage(new Page<>(pageNo, pageSize), tableName));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{tableCatalog}")
    public R info(@PathVariable("tableCatalog") String tableCatalog) {
        ColumnsEntity columns = columnsService.getById(tableCatalog);
        return R.ok().put("columns", columns);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ColumnsEntity columns) {
        columnsService.save(columns);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ColumnsEntity columns) {
        columnsService.updateById(columns);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] tableCatalogs) {
        columnsService.removeByIds(Arrays.asList(tableCatalogs));
        return R.ok();
    }

}
